package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.web.controller.demo.domain.SysMeetroom;
import com.ruoyi.web.controller.demo.domain.SysWzxUser;
import com.ruoyi.web.controller.service.ISysMeetroomService;
import com.ruoyi.web.controller.service.ISysWzxUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.web.controller.demo.domain.SysMeetManage;
import com.ruoyi.web.controller.service.ISysMeetManageService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * sys_meet_manageController
 * 
 * @author ruoyi
 * @date 2022-12-28
 */
@Controller
@RequestMapping("/system/manage")
public class SysMeetManageController extends BaseController
{
    private String prefix = "system/manage";

    @Autowired
    private ISysMeetManageService sysMeetManageService;

    @RequiresPermissions("system:manage:view")
    @GetMapping()
    public String manage()
    {
        return prefix + "/manage";
    }

    /**
     * 查询sys_meet_manage列表
     */
    @RequiresPermissions("system:manage:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysMeetManage sysMeetManage)
    {
        startPage();
        List<SysMeetManage> list = sysMeetManageService.selectSysMeetManageList(sysMeetManage);
        return getDataTable(list);
    }

    /**
     * 导出sys_meet_manage列表
     */
    @RequiresPermissions("system:manage:export")
    @Log(title = "sys_meet_manage", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysMeetManage sysMeetManage)
    {
        List<SysMeetManage> list = sysMeetManageService.selectSysMeetManageList(sysMeetManage);
        ExcelUtil<SysMeetManage> util = new ExcelUtil<SysMeetManage>(SysMeetManage.class);
        return util.exportExcel(list, "sys_meet_manage数据");
    }
    @Autowired
    private ISysWzxUserService sysWzxUserService;
    @Autowired
    private ISysMeetroomService sysMeetroomService;
    /**
     * 新增sys_meet_manage
     */
    @GetMapping("/add")
    public String add(Model model)
    {
        SysUser sysUser = getSysUser();
        model.addAttribute("user",sysUser);

        List<SysWzxUser> sysWzxUsers = sysWzxUserService.selectSysWzxUserList(null);
        model.addAttribute("users",sysWzxUsers);

        List<SysMeetroom> sysMeetrooms = sysMeetroomService.selectSysMeetroomList(null);
        model.addAttribute("meet",sysMeetrooms);

        return prefix + "/add";
    }

    /**
     * 新增保存sys_meet_manage
     */
    @RequiresPermissions("system:manage:add")
    @Log(title = "sys_meet_manage", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysMeetManage sysMeetManage)
    {
        //sysMeetManage中包括会议室id   根据id查出会议室对象列表，
        List<SysMeetManage> sysMeetManages = sysMeetManageService.selectByLocal(sysMeetManage);
        //利用sysMeetManage中的开始时间和每个会议室对象中的开始时间（1.开始是否在开始和结束之间2.开始是否等于开始）作比较
        for (SysMeetManage eve:sysMeetManages) {

            if (eve.getBeginTime().getTime() == sysMeetManage.getBeginTime().getTime() ||
                    (eve.getBeginTime().getTime() < sysMeetManage.getBeginTime().getTime() &&
                            sysMeetManage.getBeginTime().getTime() < eve.getEndTime().getTime())){
                        return AjaxResult.warn("时间安排冲突");
            }
        }
        return toAjax(sysMeetManageService.insertSysMeetManage(sysMeetManage));

    }

    /**
     * 修改sys_meet_manage
     */
    @RequiresPermissions("system:manage:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap,Model model)
    {
        SysUser sysUser = getSysUser();
        model.addAttribute("user",sysUser);

        List<SysWzxUser> sysWzxUsers = sysWzxUserService.selectSysWzxUserList(null);
        model.addAttribute("users",sysWzxUsers);

        List<SysMeetroom> sysMeetrooms = sysMeetroomService.selectSysMeetroomList(null);
        model.addAttribute("meet",sysMeetrooms);

        SysMeetManage sysMeetManage = sysMeetManageService.selectSysMeetManageById(id);
        mmap.put("sysMeetManage", sysMeetManage);
        return prefix + "/edit";
    }

    /**
     * 修改保存sys_meet_manage
     */
    @RequiresPermissions("system:manage:edit")
    @Log(title = "sys_meet_manage", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysMeetManage sysMeetManage)
    {
        return toAjax(sysMeetManageService.updateSysMeetManage(sysMeetManage));
    }

    @RequiresPermissions("system:manage:meet")
    @Log(title = "sys_meet_manage", businessType = BusinessType.UPDATE)
    @GetMapping("/open/{id}")
    @ResponseBody
    public AjaxResult openMeet(@PathVariable("id") Long id)
    {
        sysMeetManageService.openById(id);
        return AjaxResult.success("开始");
    }

    @GetMapping("/end/{id}")
    @ResponseBody
    public AjaxResult endMeet(@PathVariable("id") Long id)
    {
        sysMeetManageService.endById(id);
        return AjaxResult.success("结束");
    }

    @GetMapping("/note/{id}")
    public String noteMeet(@PathVariable("id") Long id,Model model)
    {
        SysMeetManage sysMeetManage = sysMeetManageService.selectSysMeetManageById(id);
        model.addAttribute("note",sysMeetManage);
        return prefix + "/note";
    }

    @RequiresPermissions("system:manage:edit")
    @Log(title = "sys_meet_manage", businessType = BusinessType.UPDATE)
    @PostMapping("/note")
    @ResponseBody
    public AjaxResult saveNoteMeet(SysMeetManage sysMeetManage)
    {
        //saveNoteById
        return toAjax(sysMeetManageService.saveNoteById(sysMeetManage));

    }
    /**
     * 删除sys_meet_manage
     */
    @RequiresPermissions("system:manage:remove")
    @Log(title = "sys_meet_manage", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysMeetManageService.deleteSysMeetManageByIds(ids));
    }
}
